package com.atilim.cargo.business;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.ejb.Stateless;

import com.atilim.cargo.algorithm.DijkstraAlgorithm;
import com.atilim.cargo.algorithm.Graph;
import com.atilim.cargo.model.Road;
import com.atilim.cargo.model.Vertex;
import com.atilim.cargo.test.OpenStreetMap;
import com.atilim.cargo.test.TestData;

@Stateless
public class MapBean extends BaseBean {


	/**
	 * en kısa yolu bulur ve ilgili nokta listesini döner
	 * @param nodeDestination
	 * @param nodeTarget
	 * @return
	 */
	public List<Vertex> drawToShortestPath (Vertex nodeDestination, Vertex nodeTarget) {
		
		DijkstraAlgorithm dijkstra = new DijkstraAlgorithm();
		
				
		dijkstra.computePaths(nodeDestination);
		List<Vertex> path = dijkstra.getShortestPathTo(nodeTarget);
		
		return path;
	}
	
	public Vertex locateNearestPoint (String node) {
		
		String [] values = node.split(";");
		double lat = Double.parseDouble(values [0]);
		double lng = Double.parseDouble(values [1]);
		
		Vertex closest = OpenStreetMap.findClosest(lat, lng);
		Road road = OpenStreetMap.findClosestRoad(closest, lat, lng);
		Vertex newPoint = OpenStreetMap.findPointOnRoad(lat, lng, road);
		
		Vertex otherPoint = road.getAccros(closest);
		
		closest.removeLink(otherPoint);
		closest.addLink(newPoint);
		newPoint.addLink(otherPoint);
		OpenStreetMap.vertexes.add(newPoint);
		OpenStreetMap.roads.add(new Road(closest, newPoint));
		OpenStreetMap.roads.add(new Road(newPoint, otherPoint));
		
		return newPoint;
	}
	
}
